package com.nwpu.coursesupport.service.imple;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.TaskSubmitResult;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.tool.ServiceUtil;
import com.nwpu.coursesupport.view.LessonTaskMainDetailView;
import com.nwpu.coursesupport.view.criteria.LessonTaskMainDetailViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.LessonTaskMainDetailViewMapper;

@Service
public class LessonTaskMainDetailViewService extends AbstractService{

    protected static Logger logger = LoggerFactory.getLogger(LessonTaskMainDetailViewService.class);

	
	@Autowired
	private LessonTaskMainDetailViewMapper lessonTaskMainDetailViewMapper;
	
	public JSONResult submitList(LessonTaskMainDetailViewSearchCriteria  sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		if(sc==null||sc.getAssignId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		RoleType roleType=(RoleType) getSession().getAttribute("roleType");
		if(roleType==RoleType.ASSISTANT){
			int id=(int) getSession().getAttribute("id");
			sc.setManagerId(id);
		}
		
		List<TaskSubmitResult> list=this.lessonTaskMainDetailViewMapper.submitList(sc);
		if(list==null){
			logger.error("该任务不存在");
			return rst.fail("该任务不存在");
		}
		
		rst.addData("list", list);
		rst.success();
		return rst;
	}
	
	
	public JSONResult detail(LessonTaskMainDetailViewSearchCriteria  sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		if(sc==null||sc.getAssignId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		RoleType roleType=(RoleType) getSession().getAttribute("roleType");
		if(roleType==RoleType.ASSISTANT){
			int id=(int) getSession().getAttribute("id");
			System.out.println("助教id"+ id);
			sc.setManagerId(id);
		}
		
        LessonTaskMainDetailView view=this.lessonTaskMainDetailViewMapper.detail(sc);
		if(view==null){
			logger.error("该任务不存在");
			return rst.fail("该任务不存在");
		}
        
		List<TaskSubmitResult> list=this.lessonTaskMainDetailViewMapper.submitList(sc);
		if(list==null){
			logger.error("任务分配信息获取失败");
			return rst.fail("任务分配信息获取失败");
		}
		
		
		view.setAttachment(ServiceUtil.DeserializeFiles(view.getAttachmentBytes()));
		view.setAnswerAttachment(ServiceUtil.DeserializeFiles(view.getAnswerAttachmentBytes()));
		
		
		rst.addData("list", list);
		view.setSubmits(null);
		rst.addData("view", view);
		rst.addData("type", view.getType());
		rst.success();
		return rst;
	}
}
